library(margins)
library(broom)
library(ggplot2)
library(haven)

##SWEDEN
df <- read_dta("merged_file_nomissings.dta")

# 1. marry_socdem_diff ~ treat_flag
model_socdem_diff_flag <- lm(marry_socdem_diff ~ treat_flag, data = df)
summary(model_socdem_diff_flag)


# Marginaleffekt för treat_flag i modellen marry_socdem_diff
margins_socdem_diff_flag <- margins(model_socdem_diff_flag, variables = "treat_flag")
summary(margins_socdem_diff_flag)


# 2. marry_socdem_diff ~ treat_cake
model_socdem_diff_cake <- lm(marry_socdem_diff ~ treat_cake, data = df)
summary(model_socdem_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_socdem_diff
margins_socdem_diff_cake <- margins(model_socdem_diff_cake, variables = "treat_cake")
summary(margins_socdem_diff_cake)


# 3. marry_vp_diff ~ treat_flag
model_vp_diff_flag <- lm(marry_vp_diff ~ treat_flag, data = df)
summary(model_vp_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_vp_diff
margins_vp_diff_flag <- margins(model_vp_diff_flag, variables = "treat_flag")
summary(margins_vp_diff_flag)


# 4. marry_vp_diff ~ treat_cake
model_vp_diff_cake <- lm(marry_vp_diff ~ treat_cake, data = df)
summary(model_vp_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_vp_diff
margins_vp_diff_cake <- margins(model_vp_diff_cake, variables = "treat_cake")
summary(margins_vp_diff_cake)


# 5. marry_mp_diff ~ treat_flag
model_mp_diff_flag <- lm(marry_mp_diff ~ treat_flag, data = df)
summary(model_mp_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_mp_diff
margins_mp_diff_flag <- margins(model_mp_diff_flag, variables = "treat_flag")
summary(margins_mp_diff_flag)


# 6. marry_mp_diff ~ treat_cake
model_mp_diff_cake <- lm(marry_mp_diff ~ treat_cake, data = df)
summary(model_mp_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_mp_diff
margins_mp_diff_cake <- margins(model_mp_diff_cake, variables = "treat_cake")
summary(margins_mp_diff_cake)


# 7. marry_cent_diff ~ treat_flag
model_cent_diff_flag <- lm(marry_cent_diff ~ treat_flag, data = df)
summary(model_cent_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_cent_diff
margins_cent_diff_flag <- margins(model_cent_diff_flag, variables = "treat_flag")
summary(margins_cent_diff_flag)


# 8. marry_cent_diff ~ treat_cake
model_cent_diff_cake <- lm(marry_cent_diff ~ treat_cake, data = df)
summary(model_cent_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_cent_diff
margins_cent_diff_cake <- margins(model_cent_diff_cake, variables = "treat_cake")
summary(margins_cent_diff_cake)


# 9. marry_lib_diff ~ treat_flag
model_lib_diff_flag <- lm(marry_lib_diff ~ treat_flag, data = df)
summary(model_lib_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_lib_diff
margins_lib_diff_flag <- margins(model_lib_diff_flag, variables = "treat_flag")
summary(margins_lib_diff_flag)


# 10. marry_lib_diff ~ treat_cake
model_lib_diff_cake <- lm(marry_lib_diff ~ treat_cake, data = df)
summary(model_lib_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_lib_diff
margins_lib_diff_cake <- margins(model_lib_diff_cake, variables = "treat_cake")
summary(margins_lib_diff_cake)


# 11. marry_mod_diff ~ treat_flag
model_mod_diff_flag <- lm(marry_mod_diff ~ treat_flag, data = df)
summary(model_mod_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_mod_diff
margins_mod_diff_flag <- margins(model_mod_diff_flag, variables = "treat_flag")
summary(margins_mod_diff_flag)


# 12. marry_mod_diff ~ treat_cake
model_mod_diff_cake <- lm(marry_mod_diff ~ treat_cake, data = df)
summary(model_mod_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_mod_diff
margins_mod_diff_cake <- margins(model_mod_diff_cake, variables = "treat_cake")
summary(margins_mod_diff_cake)


# 13. marry_kd_diff ~ treat_flag
model_kd_diff_flag <- lm(marry_kd_diff ~ treat_flag, data = df)
summary(model_kd_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_kd_diff
margins_kd_diff_flag <- margins(model_kd_diff_flag, variables = "treat_flag")
summary(margins_kd_diff_flag)


# 14. marry_kd_diff ~ treat_cake
model_kd_diff_cake <- lm(marry_kd_diff ~ treat_cake, data = df)
summary(model_kd_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_kd_diff
margins_kd_diff_cake <- margins(model_kd_diff_cake, variables = "treat_cake")
summary(margins_kd_diff_cake)


# 15. marry_sd_diff ~ treat_flag
model_sd_diff_flag <- lm(marry_sd_diff ~ treat_flag, data = df)
summary(model_sd_diff_flag)

# Marginaleffekt för treat_flag i modellen marry_sd_diff
margins_sd_diff_flag <- margins(model_sd_diff_flag, variables = "treat_flag")
summary(margins_sd_diff_flag)


# 16. marry_sd_diff ~ treat_cake
model_sd_diff_cake <- lm(marry_sd_diff ~ treat_cake, data = df)
summary(model_sd_diff_cake)

# Marginaleffekt för treat_cake i modellen marry_sd_diff
margins_sd_diff_cake <- margins(model_sd_diff_cake, variables = "treat_cake")
summary(margins_sd_diff_cake)


#FLAG
# --- Bygg upp dataframen för treat_flag ---
coef_list <- list(
  broom::tidy(model_socdem_diff_flag)   %>% mutate(outcome = "S"),
  broom::tidy(model_vp_diff_flag)       %>% mutate(outcome = "V"),
  broom::tidy(model_mp_diff_flag)       %>% mutate(outcome = "MP"),
  broom::tidy(model_cent_diff_flag)     %>% mutate(outcome = "C"),
  broom::tidy(model_lib_diff_flag)      %>% mutate(outcome = "L"),
  broom::tidy(model_mod_diff_flag)      %>% mutate(outcome = "M"),
  broom::tidy(model_kd_diff_flag)       %>% mutate(outcome = "KD"),
  broom::tidy(model_sd_diff_flag)       %>% mutate(outcome = "SD")
)

coef_df_flag <- do.call(rbind, coef_list) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")

coef_df_flag <- coef_df_flag %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag ---

flag_m_se <- ggplot(coef_df_flag, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Social distancing by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag marry by party SE.jpeg",
  plot     = flag_m_se
)

# CAKE
# --- Bygg upp dataframen för treat_cake ---
coef_list_cake <- list(
  broom::tidy(model_socdem_diff_cake)   %>% mutate(outcome = "S"),
  broom::tidy(model_vp_diff_cake)       %>% mutate(outcome = "V"),
  broom::tidy(model_mp_diff_cake)       %>% mutate(outcome = "MP"),
  broom::tidy(model_cent_diff_cake)     %>% mutate(outcome = "C"),
  broom::tidy(model_lib_diff_cake)      %>% mutate(outcome = "L"),
  broom::tidy(model_mod_diff_cake)      %>% mutate(outcome = "M"),
  broom::tidy(model_kd_diff_cake)       %>% mutate(outcome = "KD"),
  broom::tidy(model_sd_diff_cake)       %>% mutate(outcome = "SD")
)

coef_df_cake <- do.call(rbind, coef_list_cake) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")

coef_df_cake <- coef_df_cake %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake ---

cake_m_se <- ggplot(coef_df_cake, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Social distancing by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake marry by party SE.jpeg",
  plot     = cake_m_se
)

#####################################################
#DENMARK

#FLAG
# Förutsätter att dina data finns i en data.frame som heter `df`,
# och att variabeln treat_flag finns i df.

# 1. marry_sociald_diff ~ treat_flag
model_sociald_diff_flag <- lm(marry_sociald_diff ~ treat_flag, data = df)
summary(model_sociald_diff_flag)
margins_sociald_diff_flag <- margins(model_sociald_diff_flag, variables = "treat_flag")
summary(margins_sociald_diff_flag)

# 2. marry_venstre_diff ~ treat_flag
model_venstre_diff_flag <- lm(marry_venstre_diff ~ treat_flag, data = df)
summary(model_venstre_diff_flag)
margins_venstre_diff_flag <- margins(model_venstre_diff_flag, variables = "treat_flag")
summary(margins_venstre_diff_flag)

# 3. marry_liberal_diff ~ treat_flag
model_liberal_diff_flag <- lm(marry_liberal_diff ~ treat_flag, data = df)
summary(model_liberal_diff_flag)
margins_liberal_diff_flag <- margins(model_liberal_diff_flag, variables = "treat_flag")
summary(margins_liberal_diff_flag)

# 4. marry_socfolke_n_diff ~ treat_flag
model_socfolke_diff_flag <- lm(marry_socfolke_diff ~ treat_flag, data = df)
summary(model_socfolke_diff_flag)
margins_socfolke_diff_flag <- margins(model_socfolke_diff_flag, variables = "treat_flag")
summary(margins_socfolke_diff_flag)

# 5. marry_danmarksd_diff ~ treat_flag
model_danmarksd_diff_flag <- lm(marry_danmarksd_diff ~ treat_flag, data = df)
summary(model_danmarksd_diff_flag)
margins_danmarksd_diff_flag <- margins(model_danmarksd_diff_flag, variables = "treat_flag")
summary(margins_danmarksd_diff_flag)

# 6. marry_moderat_diff ~ treat_flag
model_moderat_diff_flag <- lm(marry_moderat_diff ~ treat_flag, data = df)
summary(model_moderat_diff_flag)
margins_moderat_diff_flag <- margins(model_moderat_diff_flag, variables = "treat_flag")
summary(margins_moderat_diff_flag)

# 7. marry_konserv_diff ~ treat_flag
model_konserv_diff_flag <- lm(marry_konserv_diff ~ treat_flag, data = df)
summary(model_konserv_diff_flag)
margins_konserv_diff_flag <- margins(model_konserv_diff_flag, variables = "treat_flag")
summary(margins_konserv_diff_flag)

# 8. marry_enheds_folke_diff ~ treat_flag
model_enheds_folke_diff_flag <- lm(marry_enheds_folke_diff ~ treat_flag, data = df)
summary(model_enheds_folke_diff_flag)
margins_enheds_folke_diff_flag <- margins(model_enheds_folke_diff_flag, variables = "treat_flag")
summary(margins_enheds_folke_diff_flag)

# 9. marry_radikale_diff ~ treat_flag
model_radikale_diff_flag <- lm(marry_radikale_diff ~ treat_flag, data = df)
summary(model_radikale_diff_flag)
margins_radikale_diff_flag <- margins(model_radikale_diff_flag, variables = "treat_flag")
summary(margins_radikale_diff_flag)

# 10. marry_danskfolk_diff ~ treat_flag
model_danskfolk_diff_flag <- lm(marry_danskfolk_diff ~ treat_flag, data = df)
summary(model_danskfolk_diff_flag)
margins_danskfolk_diff_flag <- margins(model_danskfolk_diff_flag, variables = "treat_flag")
summary(margins_danskfolk_diff_flag)

# 11. marry_alternativ_diff ~ treat_flag
model_alternativ_diff_flag <- lm(marry_alternativ_diff ~ treat_flag, data = df)
summary(model_alternativ_diff_flag)
margins_alternativ_diff_flag <- margins(model_alternativ_diff_flag, variables = "treat_flag")
summary(margins_alternativ_diff_flag)

# 12. marry_nyeborg_diff ~ treat_flag
model_nyeborg_diff_flag <- lm(marry_nyeborg_diff ~ treat_flag, data = df)
summary(model_nyeborg_diff_flag)
margins_nyeborg_diff_flag <- margins(model_nyeborg_diff_flag, variables = "treat_flag")
summary(margins_nyeborg_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
coef_list_flag_diff <- list(
  broom::tidy(model_sociald_diff_flag)      %>% mutate(outcome = "A"),
  broom::tidy(model_venstre_diff_flag)      %>% mutate(outcome = "V"),
  broom::tidy(model_liberal_diff_flag)      %>% mutate(outcome = "I"),
  broom::tidy(model_socfolke_diff_flag)     %>% mutate(outcome = "F"),
  broom::tidy(model_danmarksd_diff_flag)    %>% mutate(outcome = "Æ"),
  broom::tidy(model_moderat_diff_flag)      %>% mutate(outcome = "M"),
  broom::tidy(model_konserv_diff_flag)      %>% mutate(outcome = "C"),
  broom::tidy(model_enheds_folke_diff_flag) %>% mutate(outcome = "Ø"),
  broom::tidy(model_radikale_diff_flag)     %>% mutate(outcome = "B"),
  broom::tidy(model_danskfolk_diff_flag)    %>% mutate(outcome = "O"),
  broom::tidy(model_alternativ_diff_flag)   %>% mutate(outcome = "Å"),
  broom::tidy(model_nyeborg_diff_flag)      %>% mutate(outcome = "D")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("A", "V", "I", "F", "Æ", "M", "C", "Ø", "B", "O", "Å", "D")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---

flag_m_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Social distancing by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag marry by party DK.jpeg",
  plot     = flag_m_dk
)



#CAKE
# Förutsätter att dina data finns i en data.frame som heter `df`,
# och att variabeln treat_cake finns i df.

# 1. marry_sociald_diff ~ treat_cake
model_sociald_diff_cake <- lm(marry_sociald_diff ~ treat_cake, data = df)
summary(model_sociald_diff_cake)
margins_sociald_diff_cake <- margins(model_sociald_diff_cake, variables = "treat_cake")
summary(margins_sociald_diff_cake)

# 2. marry_venstre_diff ~ treat_cake
model_venstre_diff_cake <- lm(marry_venstre_diff ~ treat_cake, data = df)
summary(model_venstre_diff_cake)
margins_venstre_diff_cake <- margins(model_venstre_diff_cake, variables = "treat_cake")
summary(margins_venstre_diff_cake)

# 3. marry_liberal_diff ~ treat_cake
model_liberal_diff_cake <- lm(marry_liberal_diff ~ treat_cake, data = df)
summary(model_liberal_diff_cake)
margins_liberal_diff_cake <- margins(model_liberal_diff_cake, variables = "treat_cake")
summary(margins_liberal_diff_cake)

# 4. marry_socfolke_n_diff ~ treat_cake
model_socfolke_diff_cake <- lm(marry_socfolke_diff ~ treat_cake, data = df)
summary(model_socfolke_diff_cake)
margins_socfolke_diff_cake <- margins(model_socfolke_diff_cake, variables = "treat_cake")
summary(margins_socfolke_diff_cake)

# 5. marry_danmarksd_diff ~ treat_cake
model_danmarksd_diff_cake <- lm(marry_danmarksd_diff ~ treat_cake, data = df)
summary(model_danmarksd_diff_cake)
margins_danmarksd_diff_cake <- margins(model_danmarksd_diff_cake, variables = "treat_cake")
summary(margins_danmarksd_diff_cake)

# 6. marry_moderat_diff ~ treat_cake
model_moderat_diff_cake <- lm(marry_moderat_diff ~ treat_cake, data = df)
summary(model_moderat_diff_cake)
margins_moderat_diff_cake <- margins(model_moderat_diff_cake, variables = "treat_cake")
summary(margins_moderat_diff_cake)

# 7. marry_konserv_diff ~ treat_cake
model_konserv_diff_cake <- lm(marry_konserv_diff ~ treat_cake, data = df)
summary(model_konserv_diff_cake)
margins_konserv_diff_cake <- margins(model_konserv_diff_cake, variables = "treat_cake")
summary(margins_konserv_diff_cake)

# 8. marry_enheds_folke_diff ~ treat_cake
model_enheds_folke_diff_cake <- lm(marry_enheds_folke_diff ~ treat_cake, data = df)
summary(model_enheds_folke_diff_cake)
margins_enheds_folke_diff_cake <- margins(model_enheds_folke_diff_cake, variables = "treat_cake")
summary(margins_enheds_folke_diff_cake)

# 9. marry_radikale_diff ~ treat_cake
model_radikale_diff_cake <- lm(marry_radikale_diff ~ treat_cake, data = df)
summary(model_radikale_diff_cake)
margins_radikale_diff_cake <- margins(model_radikale_diff_cake, variables = "treat_cake")
summary(margins_radikale_diff_cake)

# 10. marry_danskfolk_diff ~ treat_cake
model_danskfolk_diff_cake <- lm(marry_danskfolk_diff ~ treat_cake, data = df)
summary(model_danskfolk_diff_cake)
margins_danskfolk_diff_cake <- margins(model_danskfolk_diff_cake, variables = "treat_cake")
summary(margins_danskfolk_diff_cake)

# 11. marry_alternativ_diff ~ treat_cake
model_alternativ_diff_cake <- lm(marry_alternativ_diff ~ treat_cake, data = df)
summary(model_alternativ_diff_cake)
margins_alternativ_diff_cake <- margins(model_alternativ_diff_cake, variables = "treat_cake")
summary(margins_alternativ_diff_cake)

# 12. marry_nyeborg_diff ~ treat_cake
model_nyeborg_diff_cake <- lm(marry_nyeborg_diff ~ treat_cake, data = df)
summary(model_nyeborg_diff_cake)
margins_nyeborg_diff_cake <- margins(model_nyeborg_diff_cake, variables = "treat_cake")
summary(margins_nyeborg_diff_cake)

# --- Bygg upp dataframen för treat_cake (_diff-modellerna) precis som i första fallet ---
coef_list_cake_diff <- list(
  broom::tidy(model_sociald_diff_cake)      %>% mutate(outcome = "A"),
  broom::tidy(model_venstre_diff_cake)      %>% mutate(outcome = "V"),
  broom::tidy(model_liberal_diff_cake)      %>% mutate(outcome = "I"),
  broom::tidy(model_socfolke_diff_cake)   %>% mutate(outcome = "F"),
  broom::tidy(model_danmarksd_diff_cake)    %>% mutate(outcome = "Æ"),
  broom::tidy(model_moderat_diff_cake)      %>% mutate(outcome = "M"),
  broom::tidy(model_konserv_diff_cake)      %>% mutate(outcome = "C"),
  broom::tidy(model_enheds_folke_diff_cake) %>% mutate(outcome = "Ø"),
  broom::tidy(model_radikale_diff_cake)     %>% mutate(outcome = "B"),
  broom::tidy(model_danskfolk_diff_cake)    %>% mutate(outcome = "O"),
  broom::tidy(model_alternativ_diff_cake)   %>% mutate(outcome = "Å"),
  broom::tidy(model_nyeborg_diff_cake)      %>% mutate(outcome = "D")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("A", "V", "I", "F", "Æ", "M", "C", "Ø", "B", "O", "Å", "D")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---

cake_m_dk <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Social distancing by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake marry by party DK.jpeg",
  plot     = cake_m_dk
)
